$('.index').livequery(function(){
	$('#auth-form form').submit(function() {
		var objForm = $(this);
		$.ajax({
			dataType: 'json',
			url: W_AJAX + 'user/auth-in',
			type: 'POST',
			data: {
				e_mail: $('input[type=text]', objForm).val(),
				password: $('input[type=password]', objForm).val()
			},
			success: function(data) {
				if(data.result == 'OK') location.reload();
				else alert(data.error);
			}	
		});
		return false;
	});
	
	$('#vk-auth').click(function(){
		document.location = 'https://oauth.vk.com/authorize?client_id=3299618&scope=&response_type=code&display=page&redirect_uri=http://sportcase.ru/auth/vk';
		return false;
	});
	
	$('.input-wrap').fieldClear();
});

$('body').livequery(function() {
	cityAutocomplite = {
		rightChooseFlag : false,
		init : function() {
			this.rightChooseFlag = $("#ID_city").val() != '' ? true : false;
			var self = this;
			$(".city-field").autocomplete({
				source: function(request, response) {
					self.rightChooseFlag = false;
					$.ajax({
						url: W_AJAX + 'city/auto-suggest',
						dataType: 'json',
						type: 'POST',
						data: {
							nameStartWith: request.term
						},
						success: function(data) {
							response( $.map( data.citiesNames, function(item) {
								lastCitiesResult = data.citiesNames;
								return {
									label: item.city_name,
									value: item.city_name,
									id: item.ID_city
								}
							}));
						}
					});
				},
				minLength: 2,
				focus: function() {},
				select: function( event, ui ) {
					cityAutocomplite.rightChooseFlag = true;
					alert(cityAutocomplite.rightChooseFlag);
					$('#ID_city').val(ui.item.id);
				},
				change: function( event, ui ) {
					if(!cityAutocomplite.rightChooseFlag) $(".city-field").val('');
				}
			});
		}
	}
});